home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Resources / Chat & Communication / Digsby build 37 / digsby_setup.exe / lib / ratelimited.pyo (.txt) < prev    next >
Python Compiled Bytecode  |  2008-10-13  |  3KB  |  112 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.5)
  3.  
  4. from __future__ import division
  5. import sys
  6. import time
  7. if sys.platform == 'win32':
  8.     default_timer = time.clock
  9. else:
  10.     default_timer = time.time
  11. time = time.time
  12.  
  13. class RateMonitor(object):
  14.     time_threshold = 0.5
  15.     
  16.     def __init__(self, processor):
  17.         self.f_process = processor
  18.         self.bytecounts = []
  19.         self.written = 0
  20.         self._bps = 0
  21.  
  22.     
  23.     def handle_data(self, data):
  24.         self._process(len(data))
  25.         self.f_process(data)
  26.  
  27.     
  28.     def _process(self, num_bytes):
  29.         if num_bytes > 0:
  30.             self._add_byte_data(time(), self.written + num_bytes)
  31.         
  32.  
  33.     
  34.     def bps(self):
  35.         now = time()
  36.         self._add_byte_data(now, self.written)
  37.         if len(self.bytecounts) <= 1:
  38.             self._add_byte_data(now, self.written)
  39.             self._bps = 0
  40.             return self._bps
  41.         
  42.         (oldest, lowest) = self.bytecounts[0]
  43.         (newest, highest) = self.bytecounts[-1]
  44.         time_diff = newest - oldest
  45.         byte_diff = highest - lowest
  46.         if byte_diff and time_diff > self.time_threshold:
  47.             self._bps = byte_diff / time_diff
  48.         else:
  49.             self._bps = 0
  50.         return self._bps
  51.  
  52.     bps = property(bps)
  53.     
  54.     def _add_byte_data(self, tstamp, bytecount):
  55.         self.written = bytecount
  56.         tstamp = tstamp
  57.         bytecounts = self.bytecounts
  58.         if not bytecounts:
  59.             bytecounts.append((tstamp, bytecount))
  60.         
  61.         (oldtime, oldcount) = bytecounts[-1]
  62.         if oldcount == bytecount:
  63.             bytecounts[-1] = (tstamp, bytecount)
  64.         elif tstamp > oldtime:
  65.             bytecounts.append((tstamp, bytecount))
  66.         elif tstamp == oldtime:
  67.             bytecounts[-1] = (tstamp, bytecount)
  68.         
  69.         now = time()
  70.         while bytecounts and now - bytecounts[0][0] > self.window:
  71.             bytecounts.pop(0)
  72.  
  73.  
  74.  
  75. class RateLimiter(RateMonitor):
  76.     
  77.     def __init__(self, processor, limit, window = 1):
  78.         RateMonitor.__init__(self, processor)
  79.         self.limit = limit
  80.         self.window = window
  81.         self._called_too_fast = False
  82.  
  83.     
  84.     def handle_data(self, data):
  85.         self._process(len(data))
  86.         if self.bps > self.limit:
  87.             if not self._called_too_fast:
  88.                 self.too_fast(data)
  89.                 self._called_too_fast = True
  90.             
  91.             return False
  92.         else:
  93.             self.f_process(data)
  94.             self._called_too_fast = False
  95.             return True
  96.  
  97.  
  98. if __name__ == '__main__':
  99.     
  100.     class TooFastPrinter(RateLimiter):
  101.         
  102.         def too_fast(self, data):
  103.             print 'was sending %d bytes too fast!' % len(data), self._bps
  104.  
  105.  
  106.     rl = TooFastPrinter((lambda d: pass), 20480, 1)
  107.     for i in xrange(2048):
  108.         rl.write('a')
  109.     
  110.     print len(rl.bytecounts), rl.bps, rl.written
  111.  
  112.